﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DoAnPhatTrienPhanMem.Models;


namespace DoAnPhatTrienPhanMem.Controllers
{
    public class CanBoController : BaseController
    {
        //
        // GET: /CanBo/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult DanhMucVien()
        {
            return View();
        }

        public ActionResult DanhMucPhong()
        {
            var IDphong = Request["IDphong"];
            ViewData["IDphong"] = IDphong;
            return View();
        }

        public ActionResult DanhMucBan()
        {
            var IDban = Request["IDban"];
            ViewData["IDban"] = IDban;
            return View();
        }

        public ActionResult DanhMucCanBo()
        {
            var MaVien = Request["MaVien"];
            return View();
        }

        public ActionResult AddOrEdit()
        {
            var ID = Request["ID"];
            ViewData["ID"] = "";
            if (!String.IsNullOrEmpty(ID)) ViewData["ID"] = ID;
            return View();
        }

        public ActionResult Details()
        {

            var ID = Request["ID"];
            ViewData["ID"] = ID;
            ViewData["HoTen"] = helper.GetOne<SoYeuCanBo>(s => s.ID.ToString() == ID).HoTen;
            return View();
        }

        public ActionResult PhanCong()
        {
            ViewData["IDphong"] = "";
            ViewData["IDban"] = "";
            ViewData["IDcanbo"] = "";
            var IDphong = Request["IDphong"];
            var IDban = Request["IDban"];
            var IDcanbo = Request["IDcanbo"];
            if (!String.IsNullOrEmpty(IDphong)) ViewData["IDphong"] = IDphong;
            else if (!String.IsNullOrEmpty(IDban)) ViewData["IDban"] = IDban;
            else if (!String.IsNullOrEmpty(IDcanbo)) ViewData["IDcanbo"] = IDcanbo;
            return View();
        }

        public ActionResult IndexLuong()
        {
            var ID = Request["ID"];
            ViewData["ID"] = ID;
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult Save(FormCollection values)
        {
            var result = new Dictionary<String, Object>();

            try
            {
                var ID = values["ID"];
                var entity = String.IsNullOrEmpty(ID)
                             ? new SoYeuCanBo()
                             : helper.GetOne<SoYeuCanBo>(s => s.ID.ToString() == ID);
                if (entity == null)
                {
                    return WarningMessage("Không tồn tại");
                }
                // Load data to entity
                entity = (SoYeuCanBo)GetDataFromForm(entity, values);
                if (entity.ID == Guid.Empty)
                {
                    #region Insert
                    if (helper.EntityExists<SoYeuCanBo>(s => s.SoHieuCongChuc == entity.SoHieuCongChuc))
                    {
                        return WarningMessage("Đã tồn tại");
                    }
                    entity.ID = Guid.NewGuid();
                    helper.Insert(entity);
                    #endregion
                }
                else
                {
                    #region Update
                    if (helper.EntityExists<SoYeuCanBo>(s => s.SoHieuCongChuc == entity.SoHieuCongChuc && s.ID != entity.ID))
                    {
                        return WarningMessage("Đã tồn tại");
                    }
                    helper.Update(entity, s => s.ID == entity.ID);
                    #endregion
                }
                result.Add("success", true);
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            catch (Exception e)
            {
                return WarningMessage("Lỗi :" + e.Message);
            }
        }

        public JsonResult SavePhanCong(FormCollection values)
        {
            var result = new Dictionary<String, Object>();
            try
            {
                var ID = Request["ID"];
                var MaCB = Request["MaCB"];
                ToChucCanBo entity;
                if (helper.EntityExists<ToChucCanBo>(s => s.MaCB.ToString() == MaCB))
                {
                    entity = helper.GetOne<ToChucCanBo>(s => s.MaCB.ToString() == MaCB);
                }
                else entity = new ToChucCanBo();
                if (entity == null)
                {
                    return WarningMessage("Không tồn tại");
                }
                entity = (ToChucCanBo)GetDataFromForm(entity, values);
                if (!helper.EntityExists<ToChucCanBo>(s => s.MaCB == entity.MaCB))
                {
                    #region Insert
                    if (helper.EntityExists<ToChucCanBo>(s => s.ThuocBanVien == entity.ThuocBanVien && s.MaPhongBan == entity.MaPhongBan && s.ViTri == entity.ViTri && s.ViTri != Constants.Constants.ViTri.NHANVIEN))
                    {
                        return WarningMessage("Vị trí đã tồn tại");
                    }
                    entity.ID = Guid.NewGuid();
                    helper.Insert(entity);

                    #endregion
                }
                else
                {
                    #region Update
                    if (helper.EntityExists<ToChucCanBo>(s => s.ThuocBanVien == entity.ThuocBanVien && s.MaPhongBan == entity.MaPhongBan && s.ViTri == entity.ViTri && s.ViTri != Constants.Constants.ViTri.NHANVIEN && s.MaCB != entity.MaCB))
                    {
                        return WarningMessage("Vị trí đã tồn tại");
                    }
                    entity.ID = helper.GetOne<ToChucCanBo>(s => s.MaCB == entity.MaCB).ID;
                    helper.Update(entity, s => s.MaCB == entity.MaCB);
                    #endregion
                }
                if (helper.EntityExists<QLNDNguoiDung>(s => s.MaCB == entity.MaCB))
                {
                    var user = helper.GetOne<QLNDNguoiDung>(s => s.MaCB == entity.MaCB);
                    if (entity.ViTri == Constants.Constants.ViTri.TRUONG)
                    {
                        user.QuyenTruyCap = Constants.Constants.RoleDefinition.QUANLY;
                    }
                    else user.QuyenTruyCap = Constants.Constants.RoleDefinition.CANBO;
                    helper.Update(user, s => s.MaCB == user.MaCB);
                }
                result.Add("success", true);
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            catch (Exception e)
            {
                return WarningMessage("Lỗi :" + e.Message);
            }
        }

        public JsonResult Delete(Guid ID)
        {
            IDictionary<String, Object> result = new Dictionary<String, Object>();
            try
            {
                var SanPham = helper.GetOne<SoYeuCanBo>(s => s.ID == ID);
                if (SanPham == null)
                {
                    return WarningMessage("Cán bộ không tồn tại");
                }
                helper.Delete<SoYeuCanBo>(s => s.ID == ID);
                result.Add("success", true);
            }
            catch (Exception e)
            {
                return WarningMessage("Lỗi :" + e.Message);
            }
            return Json(result);
        }


        public JsonResult GetAll()
        {
            var result = new Dictionary<String, Object>();
            var resultList = helper.GetTable<SoYeuCanBo>().OrderBy(s => s.HoTen).ToList();
            result.Add("data", resultList);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetAllOverView()
        {
            var result = new Dictionary<String, Object>();
            var resultList = helper.GetTable<CanBoOverView>().OrderBy(s => s.HoTen).ToList();
            foreach (var canbo in resultList)
            {
                if (!String.IsNullOrEmpty(canbo.ThuocBanVien))
                {
                    if (canbo.ThuocBanVien == "VIEN")
                    {
                        var phong = helper.GetOne<Phong>(s => s.MaPhong == canbo.MaPhongBan);
                        var vien = helper.GetOne<Vien>(s => s.MaVien == phong.MaVien);
                        canbo.TenDonVi = vien.TenVien + " - " + phong.TenPhong;
                    }
                    else if (canbo.ThuocBanVien == "BAN")
                    {
                        var ban = helper.GetOne<BanHanhChinh>(s => s.MaBan == canbo.MaPhongBan);
                        canbo.TenDonVi = ban.TenBan;
                    }
                }
            }
            result.Add("data", resultList);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetByID()
        {
            var ID = Request["ID"];
            var result = new Dictionary<String, Object>();
            var resultList = helper.GetTable<SoYeuCanBo>().Where(s => s.ID.ToString() == ID).ToList();
            result.Add("data", resultList);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetDetailsByID()
        {
            var ID = Request["ID"];
            var result = new Dictionary<String, Object>();
            var resultList = helper.GetTable<CanBoDetailsView>().ToList();
            resultList = resultList.Where(s => s.ID.ToString() == ID).ToList();
            foreach (var canbo in resultList)
            {
                if (!String.IsNullOrEmpty(canbo.ThuocBanVien))
                {
                    if (canbo.ThuocBanVien == "VIEN")
                    {
                        var phong = helper.GetOne<Phong>(s => s.MaPhong == canbo.MaPhongBan);
                        var vien = helper.GetOne<Vien>(s => s.MaVien == phong.MaVien);
                        canbo.TenDonVi = vien.TenVien + " - " + phong.TenPhong;
                    }
                    else if (canbo.ThuocBanVien == "BAN")
                    {
                        var ban = helper.GetOne<BanHanhChinh>(s => s.MaBan == canbo.MaPhongBan);
                        canbo.TenDonVi = ban.TenBan;
                    }
                }
            }
            result.Add("data", resultList);
            return Json(result, JsonRequestBehavior.AllowGet);
        }


        public JsonResult GetCanBo()
        {
            var result = new Dictionary<String, Object>();
            var resultList = helper.GetTable<ToChucCanBoView>().OrderBy(s => s.HoTen).ToList();
            foreach (var canbo in resultList)
            {
                if (!String.IsNullOrEmpty(canbo.ThuocBanVien))
                {
                    if (canbo.ThuocBanVien == "VIEN")
                    {
                        var phong = helper.GetOne<Phong>(s => s.MaPhong == canbo.MaPhongBan);
                        var vien = helper.GetOne<Vien>(s => s.MaVien == phong.MaVien);
                        canbo.TenBanVien = vien.TenVien + " - " + phong.TenPhong;
                    }
                    else if (canbo.ThuocBanVien == "BAN")
                    {
                        var ban = helper.GetOne<BanHanhChinh>(s => s.MaBan == canbo.MaPhongBan);
                        canbo.TenBanVien = ban.TenBan;
                    }
                }
            }
            result.Add("data", resultList);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public ToChucCanBoView GetCanBoByID(String ID)
        {
            var data = new ToChucCanBoView();
            try
            {
                data = helper.GetOne<ToChucCanBoView>(s => s.ID.ToString() == ID);
                if (data.ThuocBanVien == "VIEN")
                {
                    var phong = helper.GetOne<Phong>(s => s.MaPhong == data.MaPhongBan);
                    var vien = helper.GetOne<Vien>(s => s.MaVien == phong.MaVien);
                    data.TenBanVien = vien.TenVien + " - " + phong.TenPhong;
                }
                else if (data.ThuocBanVien == "BAN")
                {
                    var ban = helper.GetOne<BanHanhChinh>(s => s.MaBan == data.MaPhongBan);
                    data.TenBanVien = ban.TenBan;
                }
            }
            catch
            {
            }
            return data;
        }

        public ToChucCanBoView GetCanBoByMaCB(String MaCB)
        {
            var data = new ToChucCanBoView();
            try
            {
                data = helper.GetOne<ToChucCanBoView>(s => s.MaCB.ToString() == MaCB);
                if (data.ThuocBanVien == "VIEN")
                {
                    var phong = helper.GetOne<Phong>(s => s.MaPhong == data.MaPhongBan);
                    var vien = helper.GetOne<Vien>(s => s.MaVien == phong.MaVien);
                    data.TenBanVien = vien.TenVien + " - " + phong.TenPhong;
                }
                else if (data.ThuocBanVien == "BAN")
                {
                    var ban = helper.GetOne<BanHanhChinh>(s => s.MaBan == data.MaPhongBan);
                    data.TenBanVien = ban.TenBan;
                }
            }
            catch
            {
            }
            return data;
        }

        public SoYeuCanBo GetSoYeuCanBoByMaCB(String MaCB)
        {
            var data = new SoYeuCanBo();
            try
            {
                data = helper.GetOne<SoYeuCanBo>(s => s.MaCB.ToString() == MaCB);
            }
            catch
            {
            }
            return data;
        }

        public JsonResult GetByDonVi()
        {
            var IDphong = Request["IDphong"];
            var IDvien = Request["IDvien"];
            var IDban = Request["IDban"];
            var result = new Dictionary<String, Object>();
            var resultList = helper.GetTable<CanBoOverView>().OrderBy(s => s.HoTen).ToList();
            if (!String.IsNullOrEmpty(IDphong))
            {
                var MaPhong = helper.GetOne<Phong>(s => s.ID.ToString() == IDphong).MaPhong;
                resultList = resultList.Where(s => s.ThuocBanVien == "VIEN" && s.MaPhongBan == MaPhong).ToList();
            }
            else if (!String.IsNullOrEmpty(IDban))
            {
                var MaBan = helper.GetOne<BanHanhChinh>(s => s.ID.ToString() == IDban).MaBan;
                resultList = resultList.Where(s => s.ThuocBanVien == "BAN" && s.MaPhongBan == MaBan).ToList();
            }
            foreach (var canbo in resultList)
            {
                if (!String.IsNullOrEmpty(canbo.ThuocBanVien))
                {
                    if (canbo.ThuocBanVien == "VIEN")
                    {
                        var phong = helper.GetOne<Phong>(s => s.MaPhong == canbo.MaPhongBan);
                        var vien = helper.GetOne<Vien>(s => s.MaVien == phong.MaVien);
                        canbo.TenDonVi = vien.TenVien + " - " + phong.TenPhong;
                    }
                    else if (canbo.ThuocBanVien == "BAN")
                    {
                        var ban = helper.GetOne<BanHanhChinh>(s => s.MaBan == canbo.MaPhongBan);
                        canbo.TenDonVi = ban.TenBan;
                    }
                }
            }
            result.Add("data", resultList);
            return Json(result, JsonRequestBehavior.AllowGet);
        }
    }
}
